﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using INTEX.DataBase;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class ITXQVW : DocumentBase
    {
        #region-------------Properties-----------------------
        bool IsLoaded = false;
        private Dictionary<string, object> dicParamater;
        DataRow lastrow = null;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }

        ITXQVWData m_Data, m_DataX;
        ITXQVWReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
 
        BackgroundWorker bgWoker;
        public ITXQVW()
        {
            InitializeComponent();
            this.Name = "查询工具";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            IsLoaded = true;
            bgWoker = new BackgroundWorker();
            bgWoker.DoWork += bgWoker_DoWork;
            bgWoker.RunWorkerCompleted += bgWoker_RunWorkerCompleted;
            bgWoker.RunWorkerAsync();
        }
        void bgWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("更新完成", StatusEventType.StopProgress);
        }
        void bgWoker_DoWork(object sender, DoWorkEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.StartProgress);
            m_Data.Refresh();
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        { 
            try
            {
                bgWoker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                this.dicParamater = dicParamater;
                m_Data = new ITXQVWData();
                m_Data.UserID = Helpers.Helper.Left((string)dicParamater["User"] + "           ", 8); 
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                DataSet SResult = new DataSet();
                 DataRow dwData = SetData.QVW_VKPOS.NewRow();
                dwData["MANDANT"] = "00";
                if (m_Data.app.company == "YCA")
                {
                    SResult = m_Data.LoadRTData(dwData);
                }
                else
                {
                    SResult = m_Data.LoadData(dwData);
                }
                foreach (DataTable tb in SResult.Tables)
                {
                    string tbname = tb.TableName;
                    SetData.Merge(tb);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ReloadCon()
        {
            DataSet SResult = null;
            SResult = m_Data.LoadEnv();
            SetEnv.Merge(SResult);
        } 
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            SetData.Clear();
        }   
        private void btnPrint_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();

            if (setRpt == null)
                return;
            m_DataX = new ITXQVWData(dicParamater);
    
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new ITXQVWReport(dicParamater);
            m_rpt.dsSource = SetData;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }
    }
}
